<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备序列号" prop="deviceSerial" label-width="100px">
        <el-input
          v-model="queryParams.deviceSerial"
          placeholder="请输入设备序列号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="channelName">
        <el-input
          v-model="queryParams.channelName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择设备状态" clearable size="small">
          <el-option label="请选择字典生成" value=""/>
        </el-select>
      </el-form-item>
      <el-form-item label="通道号" prop="channelNo">
        <el-input
          v-model="queryParams.channelNo"
          placeholder="请输入通道号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleGet"
          v-hasPermi="['security:afDeviceCamera:add']"
        >同步列表
        </el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--plain-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
          <!--v-hasPermi="['security:afDeviceCamera:edit']"-->
        <!--&gt;修改-->
        <!--</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="danger"-->
          <!--plain-->
          <!--icon="el-icon-delete"-->
          <!--size="mini"-->
          <!--:disabled="multiple"-->
          <!--@click="handleDelete"-->
          <!--v-hasPermi="['security:afDeviceCamera:remove']"-->
        <!--&gt;删除-->
        <!--</el-button>-->
      <!--</el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['security:afDeviceCamera:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="afDeviceCameraList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--<el-table-column label="Id" align="center" prop="id" />-->
      <el-table-column label="设备序列号" align="center" prop="deviceSerial"/>
      <el-table-column label="设备名称" align="center" prop="channelName"/>
      <!--<el-table-column label="设备状态" align="center" prop="status"/>-->
      <!--<el-table-column label="申请时间" align="center" prop="addTime" width="180">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column label="通道号" align="center" prop="channelNo"/>
      <el-table-column label="监控地址" align="center" prop="pcUrl"/>
      <!--<el-table-column label="加密" align="center" prop="isEncrypt" />-->
      <!--<el-table-column label="视频级别" align="center" prop="videoLevel" />-->
      <!--<el-table-column label="许可" align="center" prop="permission" />-->
      <!--<el-table-column label="添加" align="center" prop="isAdd" />-->
      <el-table-column label="位置" align="center" prop="location" />
      <el-table-column label="编号" align="center" prop="cameraNum" />
      <el-table-column label="类型" align="center" prop="cameraType" />
      <el-table-column label="设备状态" align="center" prop="cameraStatus"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleVideo(scope.row)"
            v-hasPermi="['security:afDeviceCamera:edit']"
          >查看监控</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['security:afDeviceCamera:edit']"
          >修改</el-button>
          <!--<el-button-->
          <!--size="mini"-->
          <!--type="text"-->
          <!--icon="el-icon-delete"-->
          <!--@click="handleDelete(scope.row)"-->
          <!--v-hasPermi="['security:afDeviceCamera:remove']"-->
          <!--&gt;删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改摄像头对话框 -->
    <el-dialog :title="update.title" :visible.sync="update.open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入摄像头位置"/>
        </el-form-item>
        <el-form-item label="编号" prop="cameraNum">
          <el-input v-model="form.cameraNum" placeholder="请输入摄像头编号"/>
        </el-form-item>
        <el-form-item label="类型" prop="cameraType">
          <el-input v-model="form.cameraType" placeholder="请输入摄像头类型"/>
        </el-form-item>
        <el-form-item label="运行状态" prop="cameraStatus">
          <el-input v-model="form.cameraStatus" placeholder="请输入摄像头类型"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 摄像头监控对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <EZUIKitVedio :url="pcUrl" :token="accessToken"></EZUIKitVedio>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listAfDeviceCamera,
    getAfDeviceCamera,
    delAfDeviceCamera,
    addAfDeviceCamera,
    updateAfDeviceCamera,
    getAfDeviceCameraList,
    getAfDeviceCameraAccessToken
  } from '@/api/smartParks/security/afDeviceCamera'
  import EZUIKitVedio from '../securityVideo/EZUIKitJs.vue'

  export default {
    name: 'AfDeviceCamera',
    components: {
      EZUIKitVedio
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 摄像头表格数据
        afDeviceCameraList: [],
        // 弹出层标题
        title: '',
        pcUrl:'',
        accessToken:'',
        // 是否显示弹出层
        open: false,
        update:{
          open:false,
          title: '',
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          deviceSerial: null,
          channelName: null,
          status: null,
          channelNo: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList();
      getAfDeviceCameraAccessToken().then(response => {
        this.accessToken = response.msg;
      })
    },
    methods: {
      /** 查询摄像头列表 */
      getList() {
        this.loading = true
        listAfDeviceCamera(this.queryParams).then(response => {
          this.afDeviceCameraList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.update.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          pcUrl: null,
          location: null,
          cameraNum: null,
          cameraType: null,
          cameraStatus: null,
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.update.open = true
        this.title = '添加摄像头'
      },
      handleGet(){
        //同步成功或失败
        this.$confirm('是否确认同步摄像头列表"' + '"会删除所有数据', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return getAfDeviceCameraList()
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getAfDeviceCamera(id).then(response => {
          this.form = response.data
          this.update.open = true
          this.update.title = '修改摄像头'
        })
      },
      /** 查看监控 */
      handleVideo(row) {
        this.reset()
        this.pcUrl = row.pcUrl;
        this.open = true
        this.title = '查看监控视频'
      }
      ,
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateAfDeviceCamera(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.update.open = false
                this.getList()
              })
            } else {
              addAfDeviceCamera(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.update.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$confirm('是否确认删除摄像头编号为"' + ids + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delAfDeviceCamera(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('security/afDeviceCamera/export', {
          ...this.queryParams
        }, `security_afDeviceCamera.xlsx`)
      }
    }
  }
</script>
